
#ifndef STREAM_AUDIO_WIDGET_H
#define STREAM_AUDIO_WIDGET_H

#include <QtGui>
#include "def.h"

#include <QStringList>
#include <portaudio.h>
#include <math.h>

class StreamAudioWidget : public QGroupBox
{
	Q_OBJECT

public:
	StreamAudioWidget(const int channels, const int sampleRate);
	~StreamAudioWidget(void);

	int startCaptureOnDevice(int nDevIdx=-1); // start default input device
	int stopCaptureOnDevice();

	void setMute(bool bMuted);
	void setPlayback(bool bPlayback);

	inline bool isMuted() { return m_bMuted; }
	inline bool isPlayback() { return m_bPlayBack; }
	inline int audioChannels() { return m_nChannels; }
	inline int audioSampleRate() { return m_nSampleRate; }

	void reset(const int audioChannels, const int audioSample);

	void processAudio(const TYPE_SAMPLE_IN **data, const int size);

public slots:
	void onSetMute(int state);
	void onSetPlayback(int state);
	void onChangeSource(int idx);
	void onTimer();

signals:
	void onCaptureAudio(const TYPE_SAMPLE_IN **data, const int size, const int channels, const int sampleRate);
#if DEBUG
	void logged(const char*);
#endif

private:
	QLabel *m_lblAudio;
	QComboBox *m_cmbChannels;
	QCheckBox *m_chkMute;
	QCheckBox *m_chkPlayback;
	QProgressBar *m_prgSoundIndicator;
	QTimer *m_timerUpdate;
	int m_nEnergy;

	void createInterface();
	QStringList getListOfInputDevices();
	int getDeviceIndexByName(QString sDevName);
	QString getDeviceNameByIndex(int nDevIdx);
	int getDefaultInputDevice();

	bool m_bMuted;
	bool m_bPlayBack;

	int m_nChannels;
	int m_nSampleRate;

	/** PortAudio stream pointer */
	PaStream *stream;
};

#endif // STREAM_AUDIO_WIDGET_H
